package com.itheima.controller.backend.Service.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.controller.backend.Service.CheckGroupService;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    @Transactional
    public void addCheckGroup(CheckGroup checkGroup, String checkitemIds){
        Integer[] checkitemIdsArray = StringToArrayList(checkitemIds);



         checkGroupMapper.addCheckGroup(checkGroup);


        List<CheckItem> checkItems = new ArrayList<>();
        for (Integer checkitemId : checkitemIdsArray) {
            CheckItem checkItem = checkItemMapper.findById(checkitemId);
            checkItems.add(checkItem);
            checkGroupMapper.addCheckGroupAndCheckItem(checkGroup.getId(),checkItem.getId());
        }
        checkGroup.setCheckItems(checkItems);

    }

    @Override
    @Transactional
    public void deleteById(Integer id){
        checkGroupMapper.deleteByIdFromCheckGroupAndCheckItem(id);
        checkGroupMapper.deleteByIdFromCheckGroupAndSetmeal(id);
        checkGroupMapper.deleteById(id);
    }


    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId){
        return checkGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean, Integer currentPage, Integer pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<CheckGroup> checkGroupList = checkGroupMapper.findPage(queryPageBean);
        Page<CheckGroup> checkGroups = (Page) checkGroupList;
        return new PageResult(checkGroups.getTotal(),checkGroups.getResult());
    }

    @Override
    @Transactional
    public void edit(CheckGroup checkGroup, String checkitemIds){
        Integer[] checkitemIdsArray =  StringToArrayList(checkitemIds);

        checkItemMapper.deleteCheckgroupByCheckGroupId(checkGroup.getId());
        checkGroupMapper.edit(checkGroup);
        for (Integer checkItemId : checkitemIdsArray) {
            checkGroupMapper.addCheckGroupAndCheckItem(checkGroup.getId(),checkItemId);
        }


    }

    public List<CheckGroup> findAll(){
        return checkGroupMapper.findAll();
    }

    public CheckGroup findById(int i){
        return checkGroupMapper.findById(i);
    }


    private Integer[] StringToArrayList(String checkitemIds){
        Integer[] checkitemIdsArray = new Integer[checkitemIds.split(",").length];
        for (int i = 0; i < checkitemIdsArray.length; i++) {
            checkitemIdsArray[i] = Integer.parseInt(checkitemIds.split(",")[i]);
        }
        return checkitemIdsArray;
    }
}
